import 'package:flutter/material.dart';
import 'package:flutter_base_plugin/base_library/base/base_function.dart';
import 'package:flutter_base_plugin_example/page/cart/cart_page.dart';
import 'package:flutter_base_plugin_example/page/category/category_page.dart';
import 'package:flutter_base_plugin_example/page/main/vm/main_view_model.dart';
import 'package:flutter_base_plugin_example/page/mine/mine_page.dart';

import '../../config/k_color.dart';
import '../home/home_page.dart';

class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _mainPageState();
  }
}

class _mainPageState extends BaseProviderState<MainViewModel, MainPage> {
  @override
  void buildCreate() {
    // TODO: implement buildCreate
  }

  List itemList = [
    {
      "title": "首页",
      "icon": const Icon(
        Icons.home,
        color: KColors.norColor,
      ),
      "activeIcon": const Icon(
        Icons.home,
        color: KColors.delColor,
      ),
    },
    {
      "title": "分类",
      "icon": const Icon(
        Icons.category,
        color: KColors.norColor,
      ),
      "activeIcon": const Icon(
        Icons.category,
        color: KColors.delColor,
      ),
    },
    {
      "title": "购物车",
      "icon": const Icon(
        Icons.shopping_cart,
        color: KColors.norColor,
      ),
      "activeIcon": const Icon(
        Icons.shopping_cart,
        color: KColors.delColor,
      ),
    },
    {
      "title": "个人",
      "icon": const Icon(
        Icons.person,
        color: KColors.norColor,
      ),
      "activeIcon": const Icon(
        Icons.person,
        color: KColors.delColor,
      ),
    },
  ];
  List<Widget> tabBodies = [
    // UserPage(),

    HomePage(),
    CategoryPage(),
    const CartPage(),
    MinePage(),
  ];

  //创建页面控制器
  late PageController _pageController;

  @override
  MainViewModel buildViewModel() {
    // TODO: implement buildViewModel
    return MainViewModel();
  }

  @override
  Widget buildWidget(MainViewModel viewModel,BuildContext context) {
    _pageController = PageController(initialPage: viewModel.currentIndex);
    // TODO: implement buildWidget
    return Scaffold(
      body: IndexedStack(
        index: viewModel.currentIndex,
        children: tabBodies,
      ),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      // floatingActionButton: FloatingActionButton(
      //   onPressed: () {},
      //   child: Icon(Icons.list),
      // ),
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: true,
        iconSize: 20,
        unselectedItemColor: KColors.norColor,
        selectedItemColor: KColors.delColor,
        type: BottomNavigationBarType.fixed,
        selectedFontSize: 12,
        currentIndex: viewModel.currentIndex,
        items: _buildBottomNavigationBarItem(),
        onTap: (index) {
          viewModel.changeIndex(index);
        },
      ),
    );
  }

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem() {
    List<BottomNavigationBarItem> list = [];
    for (var element in itemList) {
      list.add(BottomNavigationBarItem(
        icon: element["icon"],
        label: element["title"],
        activeIcon: element["activeIcon"],
      ));
    }
    return list;
  }
}
